﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace FluentMVC
{
    public static class LambdaExpressions
    {
        public static string GetMemeberName<T>(this Expression<T> expression)
        {
            MemberExpression memberExpression = null;

            if (expression.Body.NodeType == ExpressionType.Convert)
            {
                var body = (UnaryExpression)expression.Body;
                memberExpression = body.Operand as MemberExpression;
            }
            else if (expression.Body.NodeType == ExpressionType.MemberAccess)
            {
                memberExpression = expression.Body as MemberExpression;
            }
            else
            {
                throw new ArgumentException("expression Argument is Error");
            }

            return memberExpression.Member.Name;
        }
    }
}
